#ifndef FASYS_FASYS_SSERVICE_OPEN_SUBSCRIPTION_MEHTOD_HPP
#define FASYS_FASYS_SSERVICE_OPEN_SUBSCRIPTION_MEHTOD_HPP

#include <fas/adv/json_rpc3/method.hpp>
//#include "../isubscriptions.hpp"
#include "../api/open_subscription_json.hpp"
#include "tags.hpp"

namespace fasys{ namespace sservice{

namespace aa = ::fas::aop;
namespace ap = ::fas::pattern;
namespace ad = ::fas::adv;
namespace ajr = ::fas::adv::json_rpc3;
namespace aj = ::fas::json;

struct open_subscription_method
{
  const char *name() const { return "open_subscription"; }

  typedef ::fasys::api::open_subscription_json open_subscription_json;
  typedef ::fasys::api::open_subscription open_subscription;

  typedef open_subscription_json invoke_notify;
  typedef open_subscription_json invoke_request;
  typedef aj::value<std::string> invoke_response;

  typedef aj::value<std::string> call_notify;
  /*typedef message_json call_request;
  typedef message_json call_response;
  */

  /*
  typedef publish_json::target request_type;
  typedef publish_json::target response_type;
  */
  


  template<typename T>
  void clear(T&) { }

  // isubscriptions* subscriptions;

  template<typename T>
  bool request(T& t, open_subscription& cmd, int id, std::string& result )
  {
    std::cout << "yo!! sservice: open_subscription METHOD " << id << std::endl;
    // virtual std::string create( const std::string& key, time_t lifetime,  const channel_list& cl ) = 0;
    result = t.subscriptions->create(cmd.key, cmd.lifetime, cmd.channels );
    t.key = cmd.key;
    t.uuid = result;
    // publisher->publish(cmd);
    return true;
  }

  template<typename T>
  void notify(T& t, open_subscription& cmd )
  {
    std::cout << "yo!! sservice: open_subscription METHOD" << std::endl;
    std::string result = t.subscriptions->create(cmd.key, cmd.lifetime, cmd.channels );
    t.get_aspect().template get<_open_subscription_>().notify(t, result );
    t.key = cmd.key;
    t.uuid = result;
    //publisher->publish(cmd);
    //t.get_aspect().template get<ajr::_writer_>()(t, (const char*)(0), size_t(0) );
  }
};

typedef aa::tag_list_n<
  aa::tag<_open_subscription_>,
  aa::gtag< ajr::_gmethod_ >,
  aa::gtag< ad::_cleaner_ >
>::type open_subscription_method_tag_list;

struct open_subscription_method_advice
  : aa::advice<
      open_subscription_method_tag_list,
      ajr::method<open_subscription_method>
    >
{};

}}

#endif
